package cn.task.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.task.po.Task;
import cn.task.po.TaskVo;
import cn.task.service.TaskService;

@Controller
@RequestMapping(value="/search")
public class SearchTaskController {
	@Autowired
	private TaskService taskService;
	@RequestMapping(value="/taskSearch")
	public @ResponseBody List<TaskVo> taskSearch(HttpServletRequest request)
	{
		TaskVo taskVo=new TaskVo();
		String type=request.getParameter("type");
		String city=request.getParameter("city");
		String commission=request.getParameter("commission");
		taskVo.setTasktype(type);
		taskVo.setCity(city);
		taskVo.setCommissionShow(commission);
		List<Task> taskLlist=taskService.selectTaskBy(taskVo);
		return copyProperties(taskLlist);
	}
	@RequestMapping(value="/byType")
	public @ResponseBody List<TaskVo> byType(HttpServletRequest request)
	{
		String param=request.getParameter("param");
		List<Task> taskLlist=taskService.selectTaskByType(param);
		return copyProperties(taskLlist);
	}
	@RequestMapping(value="/byCity")
	public @ResponseBody List<TaskVo> byCity(HttpServletRequest request)
	{
		String param=request.getParameter("param");
		List<Task> taskLlist=taskService.selectTaskByType(param);
		return copyProperties(taskLlist);
	}
	@RequestMapping(value="/byCommission")
	public @ResponseBody List<TaskVo> byCommission(HttpServletRequest request)
	{
		String param=request.getParameter("param");
		String[] params=param.split("~");
		Float value1=Float.valueOf(params[0]);
		Float value2=Float.valueOf(params[1]);
		List<Task> taskLlist=taskService.selectTaskByCommission(value1, value2);
		return copyProperties(taskLlist);
	}
	public List<TaskVo> copyProperties(List<Task> tasklist)
	{
		ArrayList<TaskVo> taskVoList=new ArrayList<TaskVo>();
		for(Task task:tasklist)
		{
			TaskVo taskVo=new TaskVo();
			Date begindate=task.getBegindate();
			Date enddate=task.getEnddate();
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
			String startDate=sdf.format(begindate);
			String overDate=sdf.format(enddate);
			taskVo.setStartDate(startDate);
			taskVo.setOverDate(overDate);
			BeanUtils.copyProperties(task,taskVo);
			taskVoList.add(taskVo);
		}
		return taskVoList;
	}
}
